/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package persistence;

import entityBeans.Georeferenceverificationstatus;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import entityBeans.Geospatialelement;
import java.util.ArrayList;
import java.util.List;
import entityBeans.Localityelement;
import persistence.exceptions.NonexistentEntityException;

/**
 *
 * @author allankv
 */
public class GeoreferenceverificationstatusJpaController {

    public GeoreferenceverificationstatusJpaController() {
        emf = Persistence.createEntityManagerFactory("bdd-javaPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public Georeferenceverificationstatus save(Georeferenceverificationstatus georeferenceverificationstatus){
        Georeferenceverificationstatus aux = findGeoreferenceverificationstatus(georeferenceverificationstatus.getGeoreferenceverificationstatus());
        if(aux==null){
            return create(georeferenceverificationstatus);
        } else
            return aux;
    }

    public Georeferenceverificationstatus create(Georeferenceverificationstatus georeferenceverificationstatus) {
        Georeferenceverificationstatus r = null;
        if (georeferenceverificationstatus.getGeospatialelementList() == null) {
            georeferenceverificationstatus.setGeospatialelementList(new ArrayList<Geospatialelement>());
        }
        if (georeferenceverificationstatus.getLocalityelementList() == null) {
            georeferenceverificationstatus.setLocalityelementList(new ArrayList<Localityelement>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            List<Geospatialelement> attachedGeospatialelementList = new ArrayList<Geospatialelement>();
            for (Geospatialelement geospatialelementListGeospatialelementToAttach : georeferenceverificationstatus.getGeospatialelementList()) {
                geospatialelementListGeospatialelementToAttach = em.getReference(geospatialelementListGeospatialelementToAttach.getClass(), geospatialelementListGeospatialelementToAttach.getIdgeospatialelement());
                attachedGeospatialelementList.add(geospatialelementListGeospatialelementToAttach);
            }
            georeferenceverificationstatus.setGeospatialelementList(attachedGeospatialelementList);
            List<Localityelement> attachedLocalityelementList = new ArrayList<Localityelement>();
            for (Localityelement localityelementListLocalityelementToAttach : georeferenceverificationstatus.getLocalityelementList()) {
                localityelementListLocalityelementToAttach = em.getReference(localityelementListLocalityelementToAttach.getClass(), localityelementListLocalityelementToAttach.getIdlocalityelement());
                attachedLocalityelementList.add(localityelementListLocalityelementToAttach);
            }
            georeferenceverificationstatus.setLocalityelementList(attachedLocalityelementList);
            r = em.merge(georeferenceverificationstatus);
            for (Geospatialelement geospatialelementListGeospatialelement : georeferenceverificationstatus.getGeospatialelementList()) {
                Georeferenceverificationstatus oldGeoreferenceverificationstatusOfGeospatialelementListGeospatialelement = geospatialelementListGeospatialelement.getGeoreferenceverificationstatus();
                geospatialelementListGeospatialelement.setGeoreferenceverificationstatus(georeferenceverificationstatus);
                geospatialelementListGeospatialelement = em.merge(geospatialelementListGeospatialelement);
                if (oldGeoreferenceverificationstatusOfGeospatialelementListGeospatialelement != null) {
                    oldGeoreferenceverificationstatusOfGeospatialelementListGeospatialelement.getGeospatialelementList().remove(geospatialelementListGeospatialelement);
                    oldGeoreferenceverificationstatusOfGeospatialelementListGeospatialelement = em.merge(oldGeoreferenceverificationstatusOfGeospatialelementListGeospatialelement);
                }
            }
            for (Localityelement localityelementListLocalityelement : georeferenceverificationstatus.getLocalityelementList()) {
                Georeferenceverificationstatus oldGeoreferenceverificationstatusOfLocalityelementListLocalityelement = localityelementListLocalityelement.getGeoreferenceverificationstatus();
                localityelementListLocalityelement.setGeoreferenceverificationstatus(georeferenceverificationstatus);
                localityelementListLocalityelement = em.merge(localityelementListLocalityelement);
                if (oldGeoreferenceverificationstatusOfLocalityelementListLocalityelement != null) {
                    oldGeoreferenceverificationstatusOfLocalityelementListLocalityelement.getLocalityelementList().remove(localityelementListLocalityelement);
                    oldGeoreferenceverificationstatusOfLocalityelementListLocalityelement = em.merge(oldGeoreferenceverificationstatusOfLocalityelementListLocalityelement);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
        return r;
    }

    public void edit(Georeferenceverificationstatus georeferenceverificationstatus) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Georeferenceverificationstatus persistentGeoreferenceverificationstatus = em.find(Georeferenceverificationstatus.class, georeferenceverificationstatus.getIdgeoreferenceverificationstatus());
            List<Geospatialelement> geospatialelementListOld = persistentGeoreferenceverificationstatus.getGeospatialelementList();
            List<Geospatialelement> geospatialelementListNew = georeferenceverificationstatus.getGeospatialelementList();
            List<Localityelement> localityelementListOld = persistentGeoreferenceverificationstatus.getLocalityelementList();
            List<Localityelement> localityelementListNew = georeferenceverificationstatus.getLocalityelementList();
            List<Geospatialelement> attachedGeospatialelementListNew = new ArrayList<Geospatialelement>();
            for (Geospatialelement geospatialelementListNewGeospatialelementToAttach : geospatialelementListNew) {
                geospatialelementListNewGeospatialelementToAttach = em.getReference(geospatialelementListNewGeospatialelementToAttach.getClass(), geospatialelementListNewGeospatialelementToAttach.getIdgeospatialelement());
                attachedGeospatialelementListNew.add(geospatialelementListNewGeospatialelementToAttach);
            }
            geospatialelementListNew = attachedGeospatialelementListNew;
            georeferenceverificationstatus.setGeospatialelementList(geospatialelementListNew);
            List<Localityelement> attachedLocalityelementListNew = new ArrayList<Localityelement>();
            for (Localityelement localityelementListNewLocalityelementToAttach : localityelementListNew) {
                localityelementListNewLocalityelementToAttach = em.getReference(localityelementListNewLocalityelementToAttach.getClass(), localityelementListNewLocalityelementToAttach.getIdlocalityelement());
                attachedLocalityelementListNew.add(localityelementListNewLocalityelementToAttach);
            }
            localityelementListNew = attachedLocalityelementListNew;
            georeferenceverificationstatus.setLocalityelementList(localityelementListNew);
            georeferenceverificationstatus = em.merge(georeferenceverificationstatus);
            for (Geospatialelement geospatialelementListOldGeospatialelement : geospatialelementListOld) {
                if (!geospatialelementListNew.contains(geospatialelementListOldGeospatialelement)) {
                    geospatialelementListOldGeospatialelement.setGeoreferenceverificationstatus(null);
                    geospatialelementListOldGeospatialelement = em.merge(geospatialelementListOldGeospatialelement);
                }
            }
            for (Geospatialelement geospatialelementListNewGeospatialelement : geospatialelementListNew) {
                if (!geospatialelementListOld.contains(geospatialelementListNewGeospatialelement)) {
                    Georeferenceverificationstatus oldGeoreferenceverificationstatusOfGeospatialelementListNewGeospatialelement = geospatialelementListNewGeospatialelement.getGeoreferenceverificationstatus();
                    geospatialelementListNewGeospatialelement.setGeoreferenceverificationstatus(georeferenceverificationstatus);
                    geospatialelementListNewGeospatialelement = em.merge(geospatialelementListNewGeospatialelement);
                    if (oldGeoreferenceverificationstatusOfGeospatialelementListNewGeospatialelement != null && !oldGeoreferenceverificationstatusOfGeospatialelementListNewGeospatialelement.equals(georeferenceverificationstatus)) {
                        oldGeoreferenceverificationstatusOfGeospatialelementListNewGeospatialelement.getGeospatialelementList().remove(geospatialelementListNewGeospatialelement);
                        oldGeoreferenceverificationstatusOfGeospatialelementListNewGeospatialelement = em.merge(oldGeoreferenceverificationstatusOfGeospatialelementListNewGeospatialelement);
                    }
                }
            }
            for (Localityelement localityelementListOldLocalityelement : localityelementListOld) {
                if (!localityelementListNew.contains(localityelementListOldLocalityelement)) {
                    localityelementListOldLocalityelement.setGeoreferenceverificationstatus(null);
                    localityelementListOldLocalityelement = em.merge(localityelementListOldLocalityelement);
                }
            }
            for (Localityelement localityelementListNewLocalityelement : localityelementListNew) {
                if (!localityelementListOld.contains(localityelementListNewLocalityelement)) {
                    Georeferenceverificationstatus oldGeoreferenceverificationstatusOfLocalityelementListNewLocalityelement = localityelementListNewLocalityelement.getGeoreferenceverificationstatus();
                    localityelementListNewLocalityelement.setGeoreferenceverificationstatus(georeferenceverificationstatus);
                    localityelementListNewLocalityelement = em.merge(localityelementListNewLocalityelement);
                    if (oldGeoreferenceverificationstatusOfLocalityelementListNewLocalityelement != null && !oldGeoreferenceverificationstatusOfLocalityelementListNewLocalityelement.equals(georeferenceverificationstatus)) {
                        oldGeoreferenceverificationstatusOfLocalityelementListNewLocalityelement.getLocalityelementList().remove(localityelementListNewLocalityelement);
                        oldGeoreferenceverificationstatusOfLocalityelementListNewLocalityelement = em.merge(oldGeoreferenceverificationstatusOfLocalityelementListNewLocalityelement);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = georeferenceverificationstatus.getIdgeoreferenceverificationstatus();
                if (findGeoreferenceverificationstatus(id) == null) {
                    throw new NonexistentEntityException("The georeferenceverificationstatus with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Georeferenceverificationstatus georeferenceverificationstatus;
            try {
                georeferenceverificationstatus = em.getReference(Georeferenceverificationstatus.class, id);
                georeferenceverificationstatus.getIdgeoreferenceverificationstatus();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The georeferenceverificationstatus with id " + id + " no longer exists.", enfe);
            }
            List<Geospatialelement> geospatialelementList = georeferenceverificationstatus.getGeospatialelementList();
            for (Geospatialelement geospatialelementListGeospatialelement : geospatialelementList) {
                geospatialelementListGeospatialelement.setGeoreferenceverificationstatus(null);
                geospatialelementListGeospatialelement = em.merge(geospatialelementListGeospatialelement);
            }
            List<Localityelement> localityelementList = georeferenceverificationstatus.getLocalityelementList();
            for (Localityelement localityelementListLocalityelement : localityelementList) {
                localityelementListLocalityelement.setGeoreferenceverificationstatus(null);
                localityelementListLocalityelement = em.merge(localityelementListLocalityelement);
            }
            em.remove(georeferenceverificationstatus);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Georeferenceverificationstatus> findGeoreferenceverificationstatusEntities() {
        return findGeoreferenceverificationstatusEntities(true, -1, -1);
    }

    public List<Georeferenceverificationstatus> findGeoreferenceverificationstatusEntities(int maxResults, int firstResult) {
        return findGeoreferenceverificationstatusEntities(false, maxResults, firstResult);
    }

    private List<Georeferenceverificationstatus> findGeoreferenceverificationstatusEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Georeferenceverificationstatus.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Georeferenceverificationstatus findGeoreferenceverificationstatus(String q) {
        Query query= getEntityManager().createNamedQuery("Georeferenceverificationstatus.findByGeoreferenceverificationstatus");
        query.setParameter("georeferenceverificationstatus",q);

        return query.getResultList().isEmpty()?null:(Georeferenceverificationstatus)query.getResultList().get(0);
    }

    public Georeferenceverificationstatus findGeoreferenceverificationstatus(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Georeferenceverificationstatus.class, id);
        } finally {
            em.close();
        }
    }

    public int getGeoreferenceverificationstatusCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Georeferenceverificationstatus> rt = cq.from(Georeferenceverificationstatus.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
